﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using HCM_maps_website.Models;

namespace HCM_maps_website.Process.DAL
{
    public class NodeService
    {
        public NodeInfo_old GetNodeInfo(int node)
        {
            using (SqlConnection sqlConnect = new SqlConnection(""))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "sp_NodeInfo_GetNodeInfo";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = sqlConnect;
                ///command.CommandTimeout = GlobalConfig.TimeOut;
                command.Parameters.AddWithValue("@node", node);
                try
                {
                    sqlConnect.Open();
                }
                catch
                {
                    throw new Exception("Can not connect to database. Please check connect");
                }
                SqlDataReader reader = command.ExecuteReader();
                NodeInfo_old nodeInfo = new NodeInfo_old();
                while (reader.Read())
                {
                    nodeInfo.Id = Convert.ToInt32(reader["id"]);
                    nodeInfo.Location = new Coordinate(Convert.ToDouble(reader["xLocation"]), Convert.ToDouble(reader["yLocation"]));
                }
                return nodeInfo;

            }
        }

        public NodeInfo_old GetNodeInfo(Coordinate location)
        {
            using (SqlConnection sqlConnect = new SqlConnection(""))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "sp_NodeInfo_GetNodeInfoLocation";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = sqlConnect;
                //command.CommandTimeout = GlobalConfig.TimeOut;
                command.Parameters.AddWithValue("@xLocation", location.Longitude);
                command.Parameters.AddWithValue("@yLocation", location.Latitude);
                try
                {
                    sqlConnect.Open();
                }
                catch
                {
                    throw new Exception("Can not connect to database. Please check connect");
                }
                SqlDataReader reader = command.ExecuteReader();
                NodeInfo_old nodeInfo = new NodeInfo_old();
                while (reader.Read())
                {
                    nodeInfo.Id = Convert.ToInt32(reader["id"]);
                    nodeInfo.Location = new Coordinate(Convert.ToDouble(reader["xLocation"]), Convert.ToDouble(reader["yLocation"]));
                }
                return nodeInfo;
            }
        }

        public int Distant(NodeInfo_old from, NodeInfo_old end)
        {
            using (SqlConnection sqlConnect = new SqlConnection(""))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "select dbo.ConvertToKM(@xStart, @yStart, @xEnd, @yEnd)";
                command.CommandType = CommandType.Text;
                command.Connection = sqlConnect;
               // command.CommandTimeout = GlobalConfig.TimeOut;
                command.Parameters.AddWithValue("@xStart", from.Location.Longitude);
                command.Parameters.AddWithValue("@yStart", from.Location.Latitude);
                command.Parameters.AddWithValue("@xEnd", end.Location.Longitude);
                command.Parameters.AddWithValue("@yEnd", end.Location.Latitude);
                try
                {
                    sqlConnect.Open();
                }
                catch
                {
                    throw new Exception("Can not connect to database. Please check connect");
                }
                double distant = Convert.ToDouble(command.ExecuteScalar());
                return Convert.ToInt32(distant*1000);
            }
        }

    }
}
